home *** CD-ROM | disk | FTP | other *** search
- //***********************************************************************
- //
- // StyleBar.cpp
- //
- //***********************************************************************
-
- #include <afxwin.h>
- #include <afxext.h>
- #include <afxcmn.h>
- #include <afxrich.h>
- #include <stdlib.h>
- #include "Resource.h"
- #include "MyWordView.h"
- #include "StyleBar.h"
-
- BEGIN_MESSAGE_MAP (CStyleBar, CToolBar)
- ON_WM_CREATE ()
- ON_CBN_SELENDOK (IDC_FONTNAMES, OnSelectFont)
- ON_CBN_SELENDOK (IDC_FONTSIZES, OnSelectSize)
- ON_CBN_CLOSEUP (IDC_FONTNAMES, OnCloseUp)
- ON_CBN_CLOSEUP (IDC_FONTSIZES, OnCloseUp)
- END_MESSAGE_MAP ()
-
- int CStyleBar::OnCreate (LPCREATESTRUCT lpcs)
- {
- static int nFontSizes[] = {
- 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 32, 36, 48, 72
- };
-
- if (CToolBar::OnCreate (lpcs) == -1)
- return -1;
-
- // Load the toolbar
- if (!LoadToolBar (IDR_STYLE_BAR))
- return -1;
-
- // Create an 8-point MS Sans Serif font for the combo boxes
- CClientDC dc (this);
- int nHeight = -((dc.GetDeviceCaps (LOGPIXELSY) * 8) / 72);
-
- m_font.CreateFont (nHeight, 0, 0, 0, FW_NORMAL, 0, 0, 0,
- DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
- DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "MS Sans Serif");
-
- CFont* pOldFont = dc.SelectObject (&m_font);
-
- TEXTMETRIC tm;
- dc.GetTextMetrics (&tm);
- int cxChar = tm.tmAveCharWidth;
- int cyChar = tm.tmHeight + tm.tmExternalLeading;
-
- dc.SelectObject (pOldFont);
-
- // Add the font name combo box to the toolbar
- SetButtonInfo (8, IDC_FONTNAMES, TBBS_SEPARATOR, cxChar * 32);
-
- CRect rect;
- GetItemRect (8, &rect);
- rect.bottom = rect.top + (cyChar * 16);
-
- if (!m_ctlNameComboBox.Create (WS_CHILD | WS_VISIBLE | WS_VSCROLL |
- CBS_DROPDOWNLIST | CBS_SORT, rect, this, IDC_FONTNAMES))
- return -1;
-
- m_ctlNameComboBox.SetFont (&m_font);
- FillNameComboBox (&dc);
-
- // Add the font size combo box to the toolbar
- SetButtonInfo (10, IDC_FONTSIZES, TBBS_SEPARATOR, cxChar * 12);
-
- GetItemRect (10, &rect);
- rect.bottom = rect.top + (cyChar * 14);
-
- if (!m_ctlSizeComboBox.Create (WS_CHILD | WS_VISIBLE | WS_VSCROLL |
- CBS_DROPDOWNLIST, rect, this, IDC_FONTSIZES))
- return -1;
-
- m_ctlSizeComboBox.SetFont (&m_font);
-
- CString string;
- for (int i=0; i<17; i++) {
- string.Format ("%d", nFontSizes[i]);
- m_ctlSizeComboBox.AddString (string);
- }
- return 0;
- }
-
- void CStyleBar::OnSelectFont ()
- {
- char szFaceName[LF_FACESIZE];
- int nIndex = m_ctlNameComboBox.GetCurSel ();
- m_ctlNameComboBox.GetLBText (nIndex, szFaceName);
-
- CWordView* pView =
- (CWordView*) ((CFrameWnd*) AfxGetMainWnd ())->GetActiveView ();
- pView->ChangeFont (szFaceName);
- }
-
- void CStyleBar::OnSelectSize ()
- {
- char szSize[8];
- int nIndex = m_ctlSizeComboBox.GetCurSel ();
- m_ctlSizeComboBox.GetLBText (nIndex, szSize);
-
- int nSize = atoi (szSize) * 20; // Need twips
-
- CWordView* pView =
- (CWordView*) ((CFrameWnd*) AfxGetMainWnd ())->GetActiveView ();
- pView->ChangeFontSize (nSize);
- }
-
- void CStyleBar::OnCloseUp ()
- {
- ((CFrameWnd*) AfxGetMainWnd ())->GetActiveView ()->SetFocus ();
- }
-
- void CStyleBar::FillNameComboBox (CDC* pDC)
- {
- ::EnumFontFamilies (pDC->m_hDC, NULL,
- (FONTENUMPROC) EnumFontNameProc, (LPARAM) this);
- }
-
- int CALLBACK CStyleBar::EnumFontNameProc (ENUMLOGFONT* lpelf,
- NEWTEXTMETRIC* lpntm, int nFontType, LPARAM lParam)
- {
- CStyleBar* pWnd = (CStyleBar*) lParam;
- if (nFontType & TRUETYPE_FONTTYPE)
- pWnd->m_ctlNameComboBox.AddString (lpelf->elfLogFont.lfFaceName);
- return 1;
- }
-
- void CStyleBar::OnUpdateCmdUI (CFrameWnd* pTarget,
- BOOL bDisableIfNoHndler)
- {
- CToolBar::OnUpdateCmdUI (pTarget, bDisableIfNoHndler);
-
- CWnd* pWnd = GetFocus ();
- if ((pWnd == &m_ctlNameComboBox) || (pWnd == &m_ctlSizeComboBox))
- return;
-
- // Get the font name and size
- int nTwips;
- char szFaceName[LF_FACESIZE];
-
- CWordView* pView =
- (CWordView*) ((CFrameWnd*) AfxGetMainWnd ())->GetActiveView ();
- pView->GetFontInfo (szFaceName, nTwips);
-
- // Update the font name combo box
- char szSelection[LF_FACESIZE];
- m_ctlNameComboBox.GetWindowText (szSelection, sizeof (szSelection));
-
- if (::lstrcmp (szFaceName, szSelection) != 0) {
- if (szFaceName[0] == 0)
- m_ctlNameComboBox.SetCurSel (-1);
- else {
- if (m_ctlNameComboBox.SelectString (-1, szFaceName) == CB_ERR)
- m_ctlNameComboBox.SetCurSel (-1);
- }
- }
-
- // Update the font size combo box
- char szSize[4];
- m_ctlSizeComboBox.GetWindowText (szSize, sizeof (szSize));
- int nSizeFromComboBox = atoi (szSize);
- int nSizeFromView = nTwips / 20;
-
- if (nSizeFromComboBox != nSizeFromView) {
- if (nTwips == -1)
- m_ctlSizeComboBox.SetCurSel (-1);
- else {
- CString string;
- string.Format ("%d", nSizeFromView);
- if (m_ctlSizeComboBox.SelectString (-1, string) == CB_ERR)
- m_ctlSizeComboBox.SetCurSel (-1);
- }
- }
- }
-